package com.vilinno.tlias.controller;

import com.vilinno.tlias.pojo.Emp;
import com.vilinno.tlias.pojo.EmpQueryParam;
import com.vilinno.tlias.pojo.PageResult;
import com.vilinno.tlias.pojo.Result;
import com.vilinno.tlias.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {

    private static final Logger log = LoggerFactory.getLogger(EmpController.class);

    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {

        log.info("分页查询：{}",empQueryParam);

        PageResult<Emp> pageResult=empService.page(empQueryParam);

        return Result.success(pageResult);

    }

    @PostMapping
    public Result add(@RequestBody Emp emp) {

        log.info("新增员工： {}",emp);

        empService.save(emp);

        return Result.success();

    }

    /**
     * 删除员工信息（一个员工和多个员工）
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){

        log.info("删除员工信息为：{}",ids);

        empService.delete(ids);

        return Result.success();

    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {

        log.info("根据员工id查询信息: {}",id);
        Emp emp=empService.getInfo(id);
        return Result.success(emp);
    }

    /**
     * 修改员工信息
     * @param emp
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工信息: {}",emp);
        empService.update(emp);
        return Result.success();
    }
}
